﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.EntityClient;

namespace EZMemoLib
{
    public class Context
    {
        //static string _LoginID;
        //public static string LoginID
        //{
        //    set
        //    {
        //        _LoginID = value;
        //    }
        //    get
        //    {
        //        return _LoginID;
        //    }
        //}

        static MemoLogin _Login;
        public static MemoLogin Login
        {
            set
            {
                _Login = value;
            }
            get
            {
                return _Login;
            }
        }


        public static string ForceFlow { set; get; }
        public static string DefaultFlow = "QP=AS";

        public static string FileFolderRoot = string.Format(@"{0}\{1}", AppDomain.CurrentDomain.BaseDirectory, "Data");
        public static string FileFolderBook( long bookID)
        {
            string folder = string.Format(@"{0}\{1}", FileFolderRoot , bookID);
            return folder;
        }

        public static string FilePathLesson( long bookID, string lessonNumber)
        {
            string folder = string.Format(@"{0}\{1}.mp3", FileFolderBook(bookID), lessonNumber);
            return folder;
        }

        public static List<MemoLogin> GetLoginList()
        {
            int rc = 0;

            var database = new EZMemoEntities();
            List<MemoLogin> loginList = database.MemoLogin.Where(c => c.Status == 1).ToList ();
            return loginList;

        }


        public static string CreateLogin (string loginName, string password)
        { 
            string hashPassword = BuildHash ( password );
            int rc = 0;

            var database = new EZMemoEntities();
            MemoLogin login = database.MemoLogin.Where(c => c.LoginName == loginName).FirstOrDefault();

            if (login == null)
            {
                login = new MemoLogin { LoginID = Guid.NewGuid().ToString(), LoginName = loginName.Trim(), LoginPassword = hashPassword, Number = 1, Status = 1, RegisterDate = DateTime.Now, RememberMe = 0 };

                database.AddToMemoLogin(login);
                rc = database.SaveChanges();
            }

            if (rc > 0)
            {
                return login.LoginID;
            }
            else
            {
                return null;
            }
        }

        public static string RemoveLogin( string loginID)
        {
            int rc = 0;

            var database = new EZMemoEntities();
            MemoLogin login = database.MemoLogin.Where(c => c.LoginID == loginID).FirstOrDefault();

            if (login != null)
            {
                database.MemoLogin.DeleteObject(login);
                rc = database.SaveChanges();
            }

            if (rc > 0)
            {
                return login.LoginID;
            }
            else
            {
                return null;
            }
        }

        public static string RememberMe(string loginName, int rememberValue)
        {
            int rc = 0;

            var database = new EZMemoEntities();
            MemoLogin login = database.MemoLogin.Where(c => c.LoginName == loginName).FirstOrDefault();

            if (login != null)
            {
                login.RememberMe = rememberValue;
                rc = database.SaveChanges();
            }

            if (rc > 0)
            {
                return login.LoginID;
            }
            else
            {
                return null;
            }
        }


        public static bool VerifyLogin(string loginName, string password)
        {
            bool isVerfied = false;
            var database = new EZMemoEntities();
            MemoLogin login = database.MemoLogin.Where(c => c.LoginName == loginName).FirstOrDefault();
            if (login != null)
            {
                string hashResult = BuildHash ( password );
                if (hashResult == login.LoginPassword)
                {
                    isVerfied = true;
                }
            }

            return isVerfied;
        }

        private static string BuildHash( string source)
        {
            string hashResult = "";
            var sha = System.Security.Cryptography.SHA1.Create();
            byte[] bytResult = sha.ComputeHash(System.Text.Encoding.UTF8.GetBytes(source));

            for (int i = 0; i < bytResult.Length; i++)
            {
                hashResult = hashResult + bytResult[i].ToString("X");
            }

            return hashResult;
        }

        public static bool IsDBConnected()
        {
            bool isOK = false;

            //options = new System.Data.Common.DbConnection.  DbConnectionOptions(newConnectionString, EntityConnectionStringBuilder.Synonyms, false);

            EntityConnection conn = new EntityConnection("name=EZMemoEntities");
            
            
            EntityConnection conn2=  ConfigOp.GetEFConnection();

            //conn.ConnectionString = "";
            // Create a long-running context with the connection.
            EZMemoEntities database = new EZMemoEntities(conn);
            string containerName = database.DefaultContainerName;


            //var database = new EZMemoEntities("name=EZMemoEntities2"); //is OK, but must write in config file



            //var database = new EZMemoEntities(ConfigOp.GetEFConnection());
            //var database = new EZMemoEntities( ConfigOp.GetEFConnectionString());
            //var database = new EZMemoEntities();

            try
            {
                database.Connection.Open();

                var connect = database.Connection;
                string constring = database.Connection.ConnectionString;
                string serverVersion = database.Connection.ServerVersion;
                string databaseName = database.Connection.Database;
                string a = database.Connection.DataSource;
                var b = database.Connection.State;

                var configList = database.MemoConfig.ToList();
                database.Connection.Close();

                isOK = true;
            }
            catch
            { 
            }

            return isOK;
        }

        public static void DBDisconnect()
        {
            try
            {
                var database = new EZMemoEntities();

                if (database.Connection != null)
                {
                    database.Connection.Close();
                }

            }
            catch
            {
            }
        }
    }
}
